<?php

namespace app\modules\clinical\models;

use Yii;
use yii\db\ActiveRecord;
use yii\web\NotFoundHttpException;

/**
 * This is the model class for table "hspt_crtinfo".
 *
 * @property integer $pk_id
 * @property integer $pt_id
 * @property integer $rel_id
 * @property string $crt_name
 * @property string $crt_time
 * @property string $pt_diagnose
 * @property string $excel_rank
 */
class Crtinfo extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'hspt_crtinfo';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pt_id', 'excel_id', 'pt_diagnose'], 'required'],
            [['pt_id', 'rel_id', 'excel_rank'], 'integer'],
            [['crt_time', 'update_at'], 'safe'],
            [['crt_name', 'update_name'], 'string', 'max' => 20],
            [['pt_diagnose'], 'string', 'max' => 30]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pk_id' => 'Pk ID',
            'pt_id' => 'Pt ID',
            'pt_diagnose' => '表格类型',
            'excel_rank' => '完成情况',
            'crt_name' => '创建人',
            'crt_time' => '创建日期',
            'update_name' => '修改人',
            'update_at' => '修改时间',

            'ppt_id' => '病案号',
            'pt_name' => '患者姓名',
            'evaluation_time' => '评估日期',
        ];
    }

    public function getExcel()
    {
        $type = Yii::$app->request->get('type', false);
        switch ($type) {
            case 'pd': $className = ExcelPd::className(); break;

            case 'et': $className = ExcelEt::className(); break;

            case 'sca': $className = ExcelSca::className(); break;

            case 'cmt': $className = ExcelCmt::className(); break;

            case 'hsp': $className = ExcelHsp::className(); break;

            case 'pfbc': $className = ExcelPfbc::className(); break;

            case 'pdf': $className = ExcelPdf::className(); break;

            case 'fcmte': $className = ExcelFcmte::className(); break;
            
            case 'udprs': $className = ExcelUdprs::className(); break;

            case 'udprs2': $className = ExcelUdprs2::className(); break;

            case 'pdx': $className = ExcelPdx::className(); break;
            
            default: throw new NotFoundHttpException('缺少参数：表格类型.'); break;
        }
        return $this->hasOne($className, ['pk_id' => 'excel_id']);
    }

    public function getPatient()
    {
        return $this->hasOne(Patient::className(), ['pk_id' => 'pt_id']);
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            
            $this->evaluation_time = $this->__clear_date($this->evaluation_time);

            if ($this->isNewRecord) {

                $this->crt_time = date('y-m-d H:i:s');
                $this->crt_name = Yii::$app->user->identity->username;
            } else {
                $this->update_at = date('y-m-d H:i:s');
                $this->update_name = Yii::$app->user->identity->username;
            }
            return true;
        }else{

            return false;
        }
    }

    static public function create($type, $excelId, $pt_rel, $evaluation_time)
    {
        $pt_id = Yii::$app->session->get('pt_id');

        $model = new Crtinfo();
        $model->pt_id = ($pt_rel == 0) ? $pt_id : $pt_rel;//if pt_rel is 0, from the origin person.
        $model->rel_id = ($pt_rel == 0) ? 0 : $pt_id;
        $model->excel_id = $excelId;
        $model->pt_diagnose = $type;
        $model->evaluation_time = $evaluation_time;

        if ($model->save()) {
            return $model->pk_id;
        } else {
            // echo var_dump($model->errors);
            return false;
        }
    }

    protected function __clear_date($date)
    {
        return preg_replace('/[_|-]*/', '', $date);
    }
}
